import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution199 {

    public static void main(String args[]) {
        Solution199 s = new Solution199();
    }

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        ArrayList<TreeNode> queue = new ArrayList<>();
        int f = 0;
        int level = 0;
        queue.add(root);
        ret.add(root.val);
        while (f < queue.size()) {
            TreeNode cur = queue.get(f);
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
            if (f == level && (f < queue.size() - 1)) {
                ret.add(queue.get(queue.size() - 1).val);
                level = queue.size() - 1;
            }
            f++;
        }
        return ret;
    }
}
